// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="Problem27.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation.  All rights reserved. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// </copyright>
// <summary>
//   Defines the Problem27 type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace EulerProblemsCSharp.Problems
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Problem 27.
    /// </summary>
    public class Problem27 : ProblemBase
    {
        /// <summary>
        /// Gets Number.
        /// </summary>
        /// <value>
        /// The number.
        /// </value>
        public override int Number
        {
            get
            {
                return 27;
            }
        }

        /// <summary>
        /// Gets Description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public override string Description
        {
            get
            {
                return "Find the sum of all the multiples of 3 or 5 below 1000.";
            }
        }

        /// <summary>
        /// Solves the problem.
        /// </summary>
        /// <returns>
        /// Solution to the problem.
        /// </returns>
        public override object Solve()
        {            
            int longestSequence = 0;
            int productOfCoeffiecients = 0;
            var primes = new HashSet<long>(new SieveOfEratosthenes().GetPrimes(1000));

            Func<int, int, int, int> formular = (a, b, n) => (n * n) + (a * n) + b;

            for (var a = -999; a <= 999; a++)
            {
                for (var b = -999; b <= 999; b++)
                {
                    var n = 0;
                    long result;
                    
                    do
                    {
                        result = formular(a, b, n);
                        n++;
                    } 
                    while (primes.Contains(result));

                    if (n > longestSequence)
                    {
                        productOfCoeffiecients = a * b;
                        longestSequence = n;
                    }
                }
            }

            return productOfCoeffiecients;
        }
    }
}